package com.lq.schoolshop.controller.before;

import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import com.lq.schoolshop.controller.BaseController;
import com.lq.schoolshop.pojo.Expand.ShopAndGoods;
import com.lq.schoolshop.service.GoodsService;
import com.lq.schoolshop.service.ShopCarService;
import com.lq.schoolshop.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author
 * @Date2021/1/12 16:15
 * @Version V1.0
 *  购物车
 **/
@Api(value = "商城前台功能--商品购物车模块",tags = "商城前台功能--商品购物车模块")
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("shopcar")
@Controller
public class ForShopCarController extends BaseController {

    @Autowired
    private ShopCarService shopCarService;

    @Autowired
    private GoodsService goodsService;

    //1.查询购物车所有信息
    @ApiOperation(value = "查询购物信息",notes = "查询购物车所有信息")
    @ResponseBody
    @RequestMapping(value = "/ShowAllShop",method = RequestMethod.GET)
    public Result ShowAllShop(@RequestHeader("define-token") String language){
        Result result = null;
        int userint = getUserIDByToken(language);
        List<ShopAndGoods> shopCars = shopCarService.selByAll(userint);
        if(shopCars!=null && shopCars.size()>0){
            result = new Result(true, StatusCode.OK,"成功",shopCars);
        }else {
            result = new Result(false,StatusCode.ERROR,"失败");
        }
        return result;
    }

    //2. 添加商品到购物车
    @ApiOperation(value = "添加商品到购物车",notes = "添加商品到购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gid",value = "商品ID",dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(name = "count",value = "商品数量",dataType = "String",required = true,paramType = "query")
    })
    @ResponseBody
    @RequestMapping(value = "/insertOrderToShopCar",method = RequestMethod.GET)
    public Result insertOrderToShopCar(@RequestParam("gid") String gid, @RequestParam("count") String count,@RequestHeader("define-token") String language){
        Result result = null;
        String userId = JwtUtil.getUserId(language);
        if((userId!=null && !userId.equals("")) && (gid!=null && !gid.equals("")) &&  (count!=null && !count.equals("")) ){
            int uidint = Integer.parseInt(userId);
            int gidint = Integer.parseInt(gid);
            int countint = Integer.parseInt(count);
            //1.查询该商品的数量够不够
            int goodscount = goodsService.selCountById(gidint);
            if(countint>=0){
            if(goodscount>=countint){     // 如果商品的数量大于或者等于  需要假如购物车商品的数量  就给予添加
                int existsgood = shopCarService.sqlByGidCount(gidint);
                if(existsgood>0){       //如果购物车已经存在该商品 则把该商品的数量+1
                    int i = shopCarService.updateGoodsCount(countint, gidint); //增加购物车商品数量
                }else {  //如果购物车不存在该商品 则添加该商品进入购物车
                    int index = shopCarService.insertAll(uidint, gidint, countint);
                }
                result = new Result(true,StatusCode.OK,"添加商品到购物车成功");
            }else {      //2.如果数量不够就给出提示  不给与添加
                result = new Result(false,StatusCode.OK,"商品库存不够,添加失败");
            }
        }else {
            result = new Result(false,StatusCode.ERROR,"失败,购物车类型转换失败");
         }
        }else {
            result = new Result(false,StatusCode.ERROR,"失败,添加商品数量为0");
        }
        return result;
    }


    //3.批量删除商品
    @ApiOperation(value = "批量删除购物车商品",notes = "批量删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "勾选商品ID",dataType = "String[]",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/delOrderToShopCar",method = RequestMethod.DELETE)
    public Result delOrderToShopCar(@RequestParam("id") String[]id){
        Result result = null;
        if(id.length<=0){
            result = new Result(false,StatusCode.OK,"请传入参数");
        }else {
            int[] intnum = new int[id.length];
            for (int i = 0; i < id.length; i++) {
                intnum[i] = Integer.parseInt(id[i]);
            }
            int index = shopCarService.deleteorderByAll(intnum);
            if(index>0){   //批量删除成功
                result = new Result(true,StatusCode.OK,"成功");

            }else {  //批量删除失败
                result = new Result(false,StatusCode.OK,"批量删除失败");
            }
        }
        return result;
    }




}
